package mains;

import java.util.List;

import javax.persistence.*;

import app.ResultMerchantCharge;

public class Main11GroupingPayments {

    private static final String PERSISTENCE_UNIT_NAME = "CashM";
    private static EntityManagerFactory factory;

    public static void main(String[] args) {
	
	factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
	EntityManager em = factory.createEntityManager();
	
	TypedQuery<ResultMerchantCharge> query = em
		.createQuery(
			"SELECT new app.ResultMerchantCharge(p.merchant.name, SUM(p.charge)) FROM Payment p GROUP BY p.merchant.name",
			ResultMerchantCharge.class);
	List<ResultMerchantCharge> result = null;
	try {
	    result = query.getResultList();
	} finally {
	    em.close();
	}
	for (ResultMerchantCharge r : result) {
	    System.out.format("%1$25s  %2$8.2f \n", r.getName(), r.getSum());
	}

    }

}
